package com.zqq.goods.presenter

import com.zqq.baselibrary.ext.excute
import com.zqq.baselibrary.http.rx.BaseSubscriber
import com.zqq.baselibrary.presenter.BasePresenter
import com.zqq.baselibrary.utils.AppPrefsUtils
import com.zqq.goods.presenter.view.GoodsDetailView
import com.zqq.goods.service.CartService
import com.zqq.goods.service.GoodsService
import com.zqq.goods.service.bean.Goods
import com.zqq.goods.service.bean.GoodsSku
import com.zqq.provider.router.RouterPath
import javax.inject.Inject

/**
 * 商品详情(SKU) Presenter
 * wc
 * @author zqq on 2019/5/30.
 */
class GoodsDetailPresenter @Inject constructor() : BasePresenter<GoodsDetailView>() {

    @Inject
    lateinit var goodsService: GoodsService

    @Inject
    lateinit var cartService: CartService

    /**
     * 获取商品详情
     */
    fun getGoodsDetailList(goodsId: Int) {
        if (!checkNetWork()) {
            return
        }
        mView.showLoading()
        goodsService.getGoodsDetail(goodsId)
                .excute(object : BaseSubscriber<Goods>(mView) {
                    override fun onNext(t: Goods) {
                        super.onNext(t)
                        mView.onGetGoodsDetailResult(t)
                    }

                    override fun onError(e: Throwable) {
                        super.onError(e)
                        val good = getGood()
                        mView.onGetGoodsDetailResult(good)
                    }

                }, lifecycleProvider)
    }


    /*
       加入购物车
    */
    fun addCart(goodsId: Int, goodsDesc: String, goodsIcon: String,
                goodsPrice: Long, goodsCount: Int, goodsSku: String) {
        if (!checkNetWork()) {
            return
        }
        mView.showLoading()
        cartService.addCart(goodsId, goodsDesc, goodsIcon, goodsPrice, goodsCount, goodsSku)
                .excute(object : BaseSubscriber<Int>(mView) {
                    override fun onNext(t: Int) {
                        super.onNext(t)
                        //保存购物车数量
                        AppPrefsUtils.putInt(RouterPath.GoodsCenter.SP_CART_SIZE, t)

                        mView.onAddCartResult(t)
                    }

                    override fun onError(e: Throwable) {
                        super.onError(e)
                        //保存购物车数量
                        AppPrefsUtils.putInt(RouterPath.GoodsCenter.SP_CART_SIZE, 8)
                        mView.onAddCartResult(5)
                    }

                }, lifecycleProvider)
    }


    fun addCart(map: Map<String, Any>) {
        if (!checkNetWork()) {
            return
        }
        mView.showLoading()
        cartService.addCart(map)
                .excute(object : BaseSubscriber<Int>(mView) {
                    override fun onNext(t: Int) {
                        super.onNext(t)
                        //保存购物车数量
                        AppPrefsUtils.putInt(RouterPath.GoodsCenter.SP_CART_SIZE, t)

                        mView.onAddCartResult(t)
                    }

                }, lifecycleProvider)
    }


    private fun getGood(): Goods {
        var icon = "https://gss2.bdstatic.com/-fo3dSag_xI4khGkpoWK1HF6hhy/baike/w%3D268%3Bg%3D0/sign=3bc7aa97e21190ef01fb95d9f620fa2b/4bed2e738bd4b31c39f3eec182d6277f9f2ff8f9.jpg"
        var banner = "http://pic32.nipic.com/20130902/13395269_103525238102_2." +
                "jpg,https://gss2.bdstatic.com/-fo3dSag_xI4khGkpoWK1HF6hhy/baike/w%3D268%3Bg%3D0/sign=3bc7aa97e21190ef01fb95d9f620fa2b/4bed2e738bd4b31c39f3eec182d6277f9f2ff8f9.jpg" +
                "http://pic32.nipic.com/20130902/13395269_103525238102_2.jpg"

        var sku1 = "红色"
        var sku2 = "蓝色"
        var sku3 = "路色"

        var skus = mutableListOf<GoodsSku>()
        skus.add(GoodsSku(22, "SKU标题-颜色", arrayListOf(sku1, sku2, sku3)))
        skus.add(GoodsSku(23, "SKU标题-大小", arrayListOf("M", "L", "XL", "XXL", "XXXL")))

        return Goods(1, 1, "商品描述", icon, 12345,
                "细节1介绍", "细节2较少静安寺", 22,
                20, "code-goods", "默认SKU（应该是选择的格式）",
                banner, skus, 2)
    }


}